﻿using DongFangGuoKai.WMSPlatform.Domain.Model;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace TokenRequestExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Test();
        }

        static async Task GetTokenAsync()
        {
            HttpClientHandler handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; // 取消证书验证
            // 创建HttpClient实例  
            using (var client = new HttpClient(handler))
            {
                // 设置请求的URL  
                string url = "http://10.104.13.225:8080/railway/async/getToken";

                // 准备POST请求的内容  
                var requestBody = new
                {
                    random = "1983",
                    secret = "09b4b46c30532b40cb2e9a1894968891c8ad0230",
                    tenantid = "cosmic",
                    timestamp = "1719976102",
                    user = "13456982386"
                };
                //添加请求头
                //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                //client.DefaultRequestHeaders.Add("Origin", "http://192.168.137.126");
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                //client.DefaultRequestHeaders.Add("Access-Control-Allow-Origin", "http://192.168.137.126");
                foreach (var header in client.DefaultRequestHeaders)
                {
                    await Console.Out.WriteLineAsync(header.Key + ":" + header.Value.FirstOrDefault());
                }

                // 将对象序列化为JSON字符串  
                string jsonRequestBody = JsonConvert.SerializeObject(requestBody);

                // 设置请求的Content-Type  
                var content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json");
                try
                {



                    // 发送POST请求  
                    HttpResponseMessage response = await client.PostAsync(url, content);
                    // 检查响应头
                    foreach (var header in response.Headers)
                    {
                        await Console.Out.WriteLineAsync(header.Key + ":" + header.Value.First());
                    }

                    // 确保HTTP成功状态值  
                    response.EnsureSuccessStatusCode();

                    // 读取响应内容  
                    string responseBody = await response.Content.ReadAsStringAsync();

                    // 输出响应结果  
                    Console.WriteLine(responseBody);
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                    throw;
                }
            }
        }

        static async Task GetTokenAsyncTow()
        {
            string url = "http://10.104.13.135:8080/railway/async/getToken"; // 替换为你的目标URL
            string requestBody = "{\"random\": \"1983\",\"secret\": \"09b4b46c30532b40cb2e9a1894968891c8ad0230\",\"tenantid\": \"cosmic\",\"timestamp\": \"1719976102\",\"user\": \"13456982386\"}";   // 替换为你的请求体内容

            HttpClient client = new HttpClient();

            var request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                string responseContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine("响应内容:");
                Console.WriteLine(responseContent);
            }
            else
            {
                Console.WriteLine($"请求失败: {response.StatusCode}");
            }
        }

        public static void Test()
        {
            string c1 = "Server=DESKTOP-35F2AS2\\MSSQLSERVER01,1455;database=HNJT_RFID;uid=sa;pwd=sa;";
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = c1,
                DbType = DbType.SqlServer,
                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoToUpper = false //禁用自动转成大写表

                }
            }, db =>
            {

                db.Aop.OnLogExecuting = (sql, pars) =>
                {

                    //获取原生SQL推荐 5.1.4.63  性能OK
                    Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

                    //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                    //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))


                };
                db.Aop.OnError = x =>
                {
                    Console.WriteLine(1111);
                };
            }
            ))
            {
                db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(TUpriverReceivingBills), typeof(TUpriverReceivingBillsDetails));
            }

        }
    }
}
